package cn.piaoyi.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.logging.*;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.OrderGoods;
import cn.piaoyi.dao.AdminDAO;
import cn.piaoyi.dao.OrderGoodsDAO;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.OrderGoodsService;


public class OrderGoodsServiceImp implements OrderGoodsService {

	private static final Log logger = LogFactory.getLog(OrderGoodsServiceImp.class);
	
	private OrderGoodsDAO orderGoodsDAO;

	public OrderGoodsDAO getOrderGoodsDAO() {
		return orderGoodsDAO;
	}

	public void setOrderGoodsDAO(OrderGoodsDAO orderGoodsDAO) {
		this.orderGoodsDAO = orderGoodsDAO;
	}

	/**
	 * 保存OrderGoods对象
	 * 
	 * @param orderGoods
	 * 			OrderGoods对象
	 * @return OrderGoods
	 * 			OrderGoods对象
	 */
	public OrderGoods createOrderGoods(OrderGoods orderGoods) throws PiaoyiException{
		try {
			orderGoods = this.getOrderGoodsDAO().saveOrderGoods(orderGoods);
			return orderGoods;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 保存OrderGoods对象
	 * 
	 * @param orderGoods
	 * 			OrderGoods对象
	 * @return OrderGoods
	 * 			OrderGoods对象
	 */
	public OrderGoods updateOrderGoods(OrderGoods orderGoods) throws PiaoyiException{
		try {
			orderGoods = this.getOrderGoodsDAO().updateOrderGoods(orderGoods);
			return orderGoods;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 根据主键ID获取OrderGoods对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return OrderGoods
	 * 			OrderGoods对象
	 */
	public OrderGoods findOrderGoodsById(long id){
		OrderGoods og =  this.getOrderGoodsDAO().findOrderGoodsById(id);
		og.setTotalPrice(og.getPrice() * og.getGoodsNumber());
		return og;
	}
	
	/**
	 * 获取OrderGoods列表
	 * 
	 * @param orderId
	 * 			long
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			OrderGoods对象列表
	 */
	public List findOrderGoodses(long orderId,String orderby, int ascOrDesc){
		List l = this.getOrderGoodsDAO().findOrderGoodses(orderId, orderby, ascOrDesc);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				OrderGoods og = (OrderGoods) l.get(i);
				og.setTotalPrice(og.getPrice() * og.getGoodsNumber());
			}
			return l;
		}
	}
	
	/**
	 * 删除OrderGoods对象
	 */
	public void removeOrderGoodsById(long id){
		this.getOrderGoodsDAO().removeOrderGoodsById(id);
	}
}
